//入口文件


"use strict"

//引包express
const express=require("express");

//引path
const path=require("path");
//引bodyparser
const bodyparser=require("body-parser");

//引入session部分的包
const session = require('express-session');



//创建app
const app=express();

//post请求时，要依赖body-parser
app.use(bodyparser.urlencoded({extended:false}));

//设置静态资源路径
app.use(express.static(path.join(__dirname,"src/statics")));

//配置session
app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 300000 }}))

//权限验证(拦截)
app.all("/*",(req,res,next)=>{
    //console.log(req.url);
    if(req.url!="/account/login" && req.url!="/account/register"){
        //权限验证
        if(req.session.username!=null){//有登陆
            next();
        }else{
            res.setHeader("Content-Type","text/html;charset=utf-8");
            res.end("<script>alert('您还没有登录,请先登录!');window.location='/account/login'</script>")
        }

    }else{
        next();
    }
})




/**
 * 1.根据不同的路由，交不同的路由来解决
 * */

//获取登录页面
const accountRouter=require(path.join(__dirname,"src/routers/accountRouter.js"));
app.use("/account",accountRouter);



//获取学生管理信息页面
const studentManagerRouter=require(path.join(__dirname,"src/routers/studentManagerRouter.js"))
app.use("/studentmanager",studentManagerRouter);


//开启服务器
app.listen(8899,"127.0.0.1",(err)=>{
    if(err){
        console.log(err);
        return;
    }

    console.log("start ok");
})

